package com.checkin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.checkin.model.Person;
import com.checkin.service.PersonService;

@Controller
@SessionAttributes("person")
public class AdminController {
	
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="/admin", method=RequestMethod.GET)
	public String getAdminPanel(Model model, HttpSession session){
		System.out.println("Admin GET dbli");
		return "admin";
	}
	@RequestMapping(value="/admin", method=RequestMethod.POST)
	public String getAdminPanelPosted(Model model, HttpSession session){
		System.out.println("Admin POST");
		return "admin";
	}
	
	@RequestMapping(value="/addVisitor", method=RequestMethod.GET)
	public String getVisitor(Model model, HttpSession session){
		
		System.out.println("addVisitor GET");
		model.addAttribute("person", new Person());
		model.addAttribute("companyList", getCompanyList());
		
		return "addVisitor";
	}
	
	@RequestMapping(value="/addVisitor", method=RequestMethod.POST)
	public String addVisitor(@ModelAttribute Person person, Model model){
		
		System.out.println("addVisitor POST");
		System.out.println(person.getFirstName());
		System.out.println(person.getLastName());
		System.out.println(person.getPhoneNumber());
		System.out.println(person.getCompany().getCompanyName());
		System.out.println(person.getVisitingDate());
		
		personService.save(person);
		
		model.addAttribute("companyList", getCompanyList());
		
		return "addVisitor";
	}
	
	@RequestMapping(value="/editCompany", method=RequestMethod.GET)
	public String getCompany(Model model, HttpSession session){

		Person person = (Person) session.getAttribute("person");
		
		if(person==null){
			person = new Person();
		}
		
		model.addAttribute("person", person);
		model.addAttribute("companyList", getCompanyList());
		
		return "addVisitor";
	}
	
	protected List<String> getCompanyList(){
		
		List<String> companyList = new ArrayList<String>();
		
		companyList.add("Cavalier IT");
		companyList.add("Microsoft");
		companyList.add("Xerox");
		companyList.add("Intel");
		companyList.add("Apple");
		
		return companyList;
	}

}
